<?php

namespace zhtfrontend\modules\sitepadapi\controllers;

use yii\console\Exception;
use yii\data\ActiveDataProvider;
use zhtfrontend\modules\sitepadapi\components\rest\ActiveController;
use yii\helpers\ArrayHelper;
use yii\web\ForbiddenHttpException;
use yii\web\HttpException;
use yii\web\NotFoundHttpException;
use yii\web\Response;

class AdminController extends ActiveController
{
    public $modelClass = 'zhtfrontend\models\datas\Admin';

    public $serializer = [
        'class' => 'zhtfrontend\modules\sitepadapi\components\rest\Serializer',
        'collectionEnvelope' => 'items',
    ];

    public function behaviors()
    {
        $behaviors = parent::behaviors();
        $behaviors['contentNegotiator']['formats']['text/html'] = Response::FORMAT_JSON;
        return $behaviors;
    }

    public function actions()
    {
        $actions = parent::actions();
        unset($actions['index'], $actions['create'], $actions['update'], $actions['delete'], $actions['view']);
        return $actions;
    }

    /*public function actionTeacher()
    {
        $modelClass = $this->modelClass;
        $query = $modelClass::find()->where(['role' => 'class_teacher']);
        return new ActiveDataProvider([
            'query' => $query
        ]);
    }

    public function actionAdmin()
    {
        $modelClass = $this->modelClass;
        $query = $modelClass::find()->where(['role' => 'administrator']);
        return new ActiveDataProvider([
            'query' => $query
        ]);
    }

    public function actionTeache()
    {
        $modelClass = $this->modelClass;
        $query = $modelClass::find()->where(['role' => 'teache_teacher']);
        return new ActiveDataProvider([
            'query' => $query
        ]);
    }*/

    public function actionView($id)
    {
        return $this->findModel($id);
    }

    public function actionFindbytel($tel)
    {
        $modelClass = $this->modelClass;
        $query = $modelClass::find()->where(['mobile' => $tel]);
        return new ActiveDataProvider([
            'query' => $query
        ]);
    }

    public function actionFindbycode($qrcode)
    {
        $modelClass = $this->modelClass;
        $query = $modelClass::find()->where(['qrcode' => $qrcode]);
        return new ActiveDataProvider([
            'query' => $query
        ]);
    }

    public function actionCreate()
    {
        $model = new $this->modelClass();
        $model->attributes = \Yii::$app->request->post();
        if (!$model->save()) {
            return array_values($model->getFirstErrors())[0];
        }
        return $model;
    }

    public function actionUpdate($id)
    {
        $model = $this->findModel($id);
        $model->attributes = \Yii::$app->request->post();
        if (!$model->save()) {
            return array_values($model->getFirstErrors())[0];
        }
        return $model;
    }

    protected function findModel($id)
    {
        $modelClass = $this->modelClass;
        if (($model = $modelClass::findOne($id)) !== null) {
            return $model;
        } else {
            try {
                throw new NotFoundHttpException('信息不存在');
            } catch (NotFoundHttpException $e) {
                return ['status' => 0, 'message' => 'error', 'result' => $e->getMessage()];
            }
        }
    }

    public function actionChangepass($id)
    {
        $data = \Yii::$app->request->post();
        $model = $this->findModel($id);
        if ($data['oldpass'] != null) {
            if (\Yii::$app->getSecurity()->validatePassword($data['oldpass'], $model->password)) {
                $model->password = \Yii::$app->security->generatePasswordHash($data['newpass']);
                $model->save(false);
                return $model;
            } else {
                try {
                    throw new ForbiddenHttpException('旧密码错误');
                } catch (ForbiddenHttpException $e) {
                    return ['status' => 0, 'message' => 'error', 'result' => $e->getMessage()];
                }
            }
        }
    }

    public function actionAdminlogin()
    {
        $data = \Yii::$app->request->post();
        $mobile = ArrayHelper::keyExists('tel', $data) ? $data['tel'] : '';
        $password = ArrayHelper::keyExists('password', $data) ? $data['password'] : '';
        if (empty($mobile) || empty($password)) {
            try {
                throw new ForbiddenHttpException('用户名或密码不能为空');
            } catch (ForbiddenHttpException $e) {
                return ['status' => 0, 'message' => 'error', 'result' => $e->getMessage()];
            }
        } else {
            $modelClass = $this->modelClass;
            $model = $modelClass::find()->where(['mobile' => $mobile, 'role' => 'administrator'])->one();
            if (empty($model)) {
                try {
                    throw new NotFoundHttpException('该用户不存在');
                } catch (NotFoundHttpException $e) {
                    return ['status' => 0, 'message' => 'error', 'result' => $e->getMessage()];
                }
            } else {
                if (\Yii::$app->getSecurity()->validatePassword($password, $model->password)) {
                    $cookies = \Yii::$app->response->cookies;
                    $cookies->add(new \yii\web\Cookie([
                        'name' => 'user_id',
                        'value' => $model->id,
                        'expire' => time() + 6004800
                    ]));
                    return ['items'=>$model];
                } else {
                    try {
                        throw new ForbiddenHttpException('密码错误');
                    } catch (ForbiddenHttpException $e) {
                        return ['status' => 0, 'message' => 'error', 'result' => $e->getMessage()];
                    }
                }
            }
        }
    }

    public function actionTeacherlogin()
    {
        $data = \Yii::$app->request->post();
        $mobile = ArrayHelper::keyExists('tel', $data) ? $data['tel'] : '';
        $password = ArrayHelper::keyExists('password', $data) ? $data['password'] : '';
        if (empty($mobile) || empty($password)) {
            try {
                throw new ForbiddenHttpException('用户名或密码不能为空');
            } catch (ForbiddenHttpException $e) {
                return ['status' => 0, 'message' => 'error', 'result' => $e->getMessage()];
            }
        } else {
            $modelClass = $this->modelClass;
            $model = $modelClass::find()->where(['mobile' => $mobile, 'role' => 'class_teacher'])->one();
            if (empty($model)) {
                try {
                    throw new NotFoundHttpException('该用户不存在');
                } catch (NotFoundHttpException $e) {
                    return ['status' => 0, 'message' => 'error', 'result' => $e->getMessage()];
                }
            } else {
                if (\Yii::$app->getSecurity()->validatePassword($password, $model->password)) {
                    $cookies = \Yii::$app->response->cookies;
                    $cookies->add(new \yii\web\Cookie([
                        'name' => 'user_id',
                        'value' => $model->id,
                        'expire' => time() + 6004800
                    ]));
                    return ['items'=>$model];
                } else {
                    try {
                        throw new ForbiddenHttpException('密码错误');
                    } catch (ForbiddenHttpException $e) {
                        return ['status' => 0, 'message' => 'error', 'result' => $e->getMessage()];
                    }
                }
            }
        }
    }

    public function actionTeachelogin()
    {
        $data = \Yii::$app->request->post();
        $mobile = ArrayHelper::keyExists('tel', $data) ? $data['tel'] : '';
        $password = ArrayHelper::keyExists('password', $data) ? $data['password'] : '';
        if (empty($mobile) || empty($password)) {
            try {
                throw new ForbiddenHttpException('用户名或密码不能为空');
            } catch (ForbiddenHttpException $e) {
                return ['status' => 0, 'message' => 'error', 'result' => $e->getMessage()];
            }
        } else {
            $modelClass = $this->modelClass;
            $model = $modelClass::find()->where(['mobile' => $mobile, 'role' => 'teache_teacher'])->one();
            if (empty($model)) {
                try {
                    throw new NotFoundHttpException('该用户不存在');
                } catch (NotFoundHttpException $e) {
                    return ['status' => 0, 'message' => 'error', 'result' => $e->getMessage()];
                }
            } else {
                if (\Yii::$app->getSecurity()->validatePassword($password, $model->password)) {
                    $cookies = \Yii::$app->response->cookies;
                    $cookies->add(new \yii\web\Cookie([
                        'name' => 'user_id',
                        'value' => $model->id,
                        'expire' => time() + 6004800
                    ]));
                    return ['items'=>$model];
                } else {
                    try {
                        throw new ForbiddenHttpException('密码错误');
                    } catch (ForbiddenHttpException $e) {
                        return ['status' => 0, 'message' => 'error', 'result' => $e->getMessage()];
                    }
                }
            }
        }
    }


}
